package com.test;

import ij.process.ColorProcessor;

import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;

import com.iamk.feature.FeaturesColor;
import com.iamk.util.CreateImageRegion;
import com.iamk.util.HaarTransform;
import com.iamk.util.ImageUtil;
import com.iamk.util.ListFilesUtil;

import de.lmu.ifi.dbs.jfeaturelib.features.Tamura;


public class CreateDataWavelet {
	public static void main(String[] args) {
		NumberFormat nf = new DecimalFormat("#.######");
		BufferedWriter bw = null;
		ListFilesUtil lfu = new ListFilesUtil();
		lfu.listAllFiles("C:\\Users\\Ken\\Desktop\\data-mscr-7class\\Images\\");
		HaarTransform mHaar = new HaarTransform();
		CreateImageRegion cir;
		try {
			bw = new BufferedWriter(new FileWriter("C:\\Users\\Ken\\Desktop\\data-mscr-7class\\Features\\Wavelet\\features-wavelet2-tamura-dcd-7-msrc.arff",true));
			// Writer header file arff
			bw.write("@RELATION feature-wavelet2-tamura-dcd\n");
			for (int i = 0; i < 57; i++){
				bw.write("@ATTRIBUTE f" + (i + 1) + "	REAL\n");
			}
			bw.write("@attribute class {");
			for(int i = 0; i < 21; i++){
				int temp = i;
				temp++;
				if(i == 20){
					bw.write(temp + "");
				}else{
					bw.write(temp + ",");
				}
				
			}
			bw.write("}\n");
			bw.write("@data\n");
//			System.out.println(lfu.arrAllFile.size());
			for(int f = 0; f < lfu.arrAllFile.size();f++){
				System.out.println(lfu.arrFileName.get(f));
				String filename = lfu.arrFileName.get(f).replace(".jpg", "");
				filename = filename.replace(".bmp", "");
				cir = new CreateImageRegion(lfu.arrAllFile.get(f), "C:\\Users\\Ken\\Desktop\\data-mscr-7class\\Mask\\"+filename+".txt");
				cir.loadData(cir.imgPath, cir.maskPath);
				
				for(int i = 0; i < cir.listRegion.size(); i++){
					BufferedImage imgWavelet = mHaar.foward(ImageUtil.imageToBufferedImage(cir.listRegion.get(i)), 2);
					BufferedImage imgApp2 = imgWavelet.getSubimage(0, 0, imgWavelet.getWidth()/4, imgWavelet.getHeight()/4);
					BufferedImage imgD2 = imgWavelet.getSubimage(imgWavelet.getWidth()/4, imgWavelet.getHeight()/4, imgWavelet.getWidth()/4, imgWavelet.getHeight()/4);
					BufferedImage imgD1 = imgWavelet.getSubimage(imgWavelet.getWidth()/2, imgWavelet.getHeight()/2, imgWavelet.getWidth()/2, imgWavelet.getHeight()/2);
					
					Tamura mDescriptorApp2 = new Tamura();
			        mDescriptorApp2.run(new ColorProcessor(imgApp2));
			        List<double[]> featuresApp2 = mDescriptorApp2.getFeatures();
					for (double[] feature : featuresApp2) {
						for(int l = 0; l < feature.length; l++){
							bw.write(nf.format(feature[l]) + ", ");
						}
					}
					
					Tamura mDescriptorD2 = new Tamura();
			        mDescriptorD2.run(new ColorProcessor(imgD2));
			        List<double[]> featuresD2 = mDescriptorD2.getFeatures();
					for (double[] feature : featuresD2) {
						for(int l = 0; l < feature.length; l++){
							bw.write(nf.format(feature[l]) + ", ");
						}
					}
					
					Tamura mDescriptorD1 = new Tamura();
			        mDescriptorD1.run(new ColorProcessor(imgD1));
			        List<double[]> featuresD1 = mDescriptorD1.getFeatures();
					for (double[] feature : featuresD1) {
						for(int l = 0; l < feature.length; l++){
							bw.write(nf.format(feature[l]) + ", ");
						}
					}
					
					FeaturesColor mColorDescriptorApp2 = new FeaturesColor(imgApp2);
			        List<Double> myfeaturesApp2 = mColorDescriptorApp2.getFeatures();
					for (int j = 0; j < myfeaturesApp2.size(); j++) {
						bw.write(nf.format(myfeaturesApp2.get(j)) + ", ");
					}
					
//					FeaturesColor mColorDescriptorD2 = new FeaturesColor(imgD2);
//			        List<Double> myfeaturesD2 = mColorDescriptorD2.getFeatures();
//					for (int j = 0; j < myfeaturesD2.size(); j++) {
//						bw.write(nf.format(myfeaturesD2.get(j)) + ", ");
//					}
//					FeaturesColor mColorDescriptorD1 = new FeaturesColor(imgD1);
//			        List<Double> myfeaturesD1 = mColorDescriptorD1.getFeatures();
//					for (int j = 0; j < myfeaturesD1.size(); j++) {
//						bw.write(nf.format(myfeaturesD1.get(j)) + ", ");
//					}
					bw.write(cir.listLabel.get(i) + "\n");
				}
			}
			bw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
